<template>
  <div class="mod-config">
    <basic-container>
      <navigation :sign="{ a: true, b: false, c: false }"></navigation>
      <div class="boxsty zhiBiao align-right">
        <el-button type="primary" @click="$router.back()">返回</el-button>
        <el-button type="primary">复用考核指标</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
      <div class="boxsty zhiBiao mt-24">
        <div class="flex justifyContentSpaceBetween">
          <div class="tabletext">指标考核创建</div>
        </div>
        <el-form :inline="true" :model="form" label-width="150px">
          <el-form-item label="考核名称">
            <el-input v-model="form.name" placeholder="考核名称" />
          </el-form-item>
          <el-form-item label="审核流程">
            <el-select v-model="form.processId"> </el-select>
          </el-form-item>
          <el-form-item label="权重总值">
            <el-input v-model="form.qz" placeholder="权重总值" />
          </el-form-item>
          <el-form-item label="被考核对象类型">
            <el-select v-model="form.objType">
              <el-option label="个人" value="PERSON"></el-option>
              <el-option label="组" value="GROUP"></el-option>
              <el-option label="机构" value="DEPT"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="被考核对象">
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="showAdd"
            ></el-button>
            <div class="flex align-center">
              <div v-for="(e, i) in objs">{{ e.ryName }}</div>
            </div>
          </el-form-item>
          <el-form-item label="是否验证权重">
            <el-radio-group v-model="form.checkQz">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="多人考核模式">
            <el-input v-model="form.mode" placeholder="多人考核模式" />
          </el-form-item>
          <el-form-item label="考核有效日期">
            <el-date-picker
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div class="boxsty zhiBiao mt-24">
        <div class="flex justifyContentSpaceBetween">
          <div class="tabletext">考核指标维护</div>
        </div>
        <el-table border stripe :data="form.norms">
          <el-table-column
            prop="name"
            label="序号"
            :formatter="nShowIndex"
          ></el-table-column>
          <el-table-column prop="normLx" label="指标类别"></el-table-column>
          <el-table-column prop="normName" label="指标名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="normValue" label="目标值"></el-table-column>
          <el-table-column prop="normDw" label="单位"></el-table-column>
          <el-table-column prop="name" label="权重">
            <template slot-scope="scope">
              <el-input v-model="scope.row.qz"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="完成值"> </el-table-column>
          <el-table-column prop="normGs" label="计算规则"></el-table-column>
          <el-table-column
            prop="createDeptName"
            label="数据提供部门"
          ></el-table-column>
          <el-table-column prop="name" label="自评指标"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot="header" slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-plus"
                @click="zbShow = true"
              ></el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                type="primary"
                plain
                icon="el-icon-minus"
                @click="delZb(scope.$index)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </basic-container>
    <zb :visible.sync="zbShow" @cancel="zbShow = false" @submit="addZb" />
    <user
      v-if="form.objType == 'PERSON'"
      :visible.sync="objShow"
      @cancel="objShow = false"
      @submit="addObj"
    />
    <dept
      v-else-if="form.objType == 'DEPT'"
      :visible.sync="objShow"
      @cancel="objShow = false"
      @submit="addObj"
    />
    <dept
      v-else-if="form.objType == 'GROUP'"
      :visible.sync="objShow"
      @cancel="objShow = false"
      @submit="addObj"
    />
  </div>
</template>

<script>
import zb from "@/views/assessment-index/template/components/zb";
import user from "@/views/assessment-index/template/components/user";
import dept from "@/views/assessment-index/template/components/dept";
import group from "@/views/assessment-index/template/components/group";
import { mapGetters } from "vuex";
import { addOrUpdateAssessment } from "@/api/assessment/index";
export default {
  data() {
    return {
      zbShow: false,
      objShow: false,
      objs: [],
      form: {
        objType: "",
        norms: [],
        objIds: [],
      },
    };
  },
  components: {
    zb,
    user,
    dept,
    group,
  },
  created() {},
  mounted() {},
  computed: {
    ...mapGetters(["permissions"]),
  },
  methods: {
    showAdd() {
      if (!this.form.objType) {
        this.$message.error("先选择考核对象类型");
      }
      this.objShow = true;
    },
    addObj(data) {
      this.objs = data;
      if (this.form.objType == "PERSON") {
        this.form.objIds = data.map((v) => v.userId);
      } else if (this.form.objType == "DEPT") {
        this.form.objIds = data.map((v) => v.id);
      }
      this.objShow = false;
    },
    addZb(rows) {
      this.form.norms = rows;
      this.zbShow = false;
    },
    delZb(index) {
      this.form.norms.splice(index, 1);
    },
    nShowIndex(row, column, cellValue, index) {
      return index + 1;
    },
    save() {
      addOrUpdateAssessment(this.form).then((res) => {
       //console.log(res);
      });
    },
  },
};
</script>

<style scoped>
.align-right {
  text-align: right;
}
.mt-24 {
  margin-top: 24px;
}
</style>
